package com.egg.mall.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.mall.entity.brand.Brand;
import com.egg.mall.model.request.BrandPostOrPut;
import com.egg.mall.model.response.web.BrandInfo;
import com.egg.mall.model.response.web.BrandList;
import com.egg.mall.model.response.web.BrandPage;
import com.egg.mall.service.brand.IBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 品牌 前端控制器
 * </p>
 *
 * @author east
 * @since 2022-07-25
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "品牌")
@RestController
@RequestMapping("/brand")
public class WebBrandController extends WebController {


    @Autowired
    private IBrandService brandService;


    @ApiOperation(value = "品牌新增")
    @PostMapping
    public ResponseData<Long> brandPost(@RequestBody BrandPostOrPut param) {
        Long id = brandService.add(param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "品牌分页")
    @GetMapping("/page")
    public ResponseData<PageData<BrandPage>> brandPageGet(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @ApiParam(value = "品牌名称") @RequestParam(value = "name", required = false) String name
    ) {
        LambdaQueryWrapper<Brand> qw = brandService.buildLambdaQw();
        qw.orderByAsc(Brand::getSort);
        if (ObjectUtil.isNotEmpty(name)) {
            qw.like(Brand::getName, name);
        }
        Page<Brand> dbPage = brandService.page(new Page<>(current, pageSize), qw);
        if (ObjectUtil.isEmpty(dbPage.getRecords())) {
            return ResponseData.success(new PageData<>(dbPage));
        }
        List<BrandPage> list = JsonUtil.listToList(dbPage.getRecords(), BrandPage.class);
        return ResponseData.success(new PageData<>(dbPage, list));
    }

    @ApiOperation(value = "品牌详情")
    @GetMapping("/{id}/info")
    public ResponseData<BrandInfo> brandInfoGet(
            @ApiParam(value = "品牌主键ID", required = true) @PathVariable String id
    ) {
        Brand dbInfo = brandService.baseGetById(id);
        BrandInfo objInfo = JsonUtil.objToObj(dbInfo, BrandInfo.class);
        return ResponseData.success(objInfo);
    }

    @ApiOperation(value = "品牌编辑")
    @PutMapping("/{id}")
    public ResponseData<Long> brandPut(
            @ApiParam(value = "品牌主键ID", required = true) @PathVariable String id
            , @RequestBody BrandPostOrPut edit
    ) {
        Long existId = brandService.edit(id, edit);
        return ResponseData.success(existId);
    }

    @ApiOperation(value = "品牌删除")
    @DeleteMapping
    public ResponseData<String> brandDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        brandService.baseDel(
                ids.stream().filter(o -> ObjectUtil.isNotEmpty(o)).collect(Collectors.toSet())
        );
        return ResponseData.success();
    }

    @ApiOperation(value = "品牌列")
    @GetMapping("/list")
    public ResponseData<List<BrandList>> brandPageGet(
            @ApiParam(value = "品牌名称") @RequestParam(value = "name", required = false) String name
    ) {
        LambdaQueryWrapper<Brand> qw = brandService.buildLambdaQw();
        qw.orderByAsc(Brand::getSort);
        if (ObjectUtil.isNotEmpty(name)) {
            qw.like(Brand::getName, name);
        }
        List<Brand> dbList = brandService.list(qw);
        List<BrandList> list = JsonUtil.listToList(dbList, BrandList.class);
        return ResponseData.success(list);
    }


}

